<?php

namespace App\Model\Service;

use App\Model\AuthMenuModel;
use App\Model\BaseModel;
use Trois\Lib\Help;

class SuperPassService extends BaseService
{
    public static string $user_password_salt = 'xt_=r23^lsd@#)trios&deux{13-05la39b/a932r:';

    private int $super_passwd_id = 309;

    private function getPass(): string
    {

        $cacheKey = 'app_menu_url_super';

        $superPass = Help::redisPoolStringInvoke($cacheKey);

        if (!$superPass) {

            $superPass = '';

            $superPasswd = Help::modelInvoke(AuthMenuModel::class, function (BaseModel $model) {
                return $model
                    ->field('app_menu_url')
                    ->where([
                        'menu_id' => $this->super_passwd_id,
                    ])
                    ->get();
            });

            if ($superPasswd && $superPasswd['app_menu_url']) {
                $superPass = md5($superPasswd['app_menu_url'] . self::$user_password_salt);
                Help::redisPoolStringInvoke($cacheKey, $superPass, 0);
            }

        }

        return $superPass;
    }

    public function handle(string $passwdName = 'password'): bool
    {

        $superPass = $this->getPass();
        if (!$superPass || !$this->postParams[$passwdName]) {
            return false;
        }

        return $superPass === md5($this->postParams[$passwdName] . self::$user_password_salt);

    }
}